Trò chơi game bắn súng đơn giản trong UNITY Engine
23.626 lượt xem;
1 using UnityEngine;
2 using System.Collections;
3
4 public class SpawnManager : MonoBehaviour
5 {
6 public Transform strtSpawn, endSpawn;
7 float[] pointsPositions;
8 Pools pools;
9 Random rand;
10
11 void Awake()
12 {
13 pools = GameObject.FindObjectOfType<Pools>();
14 rand = new Random();
15 }
16
17 // Use this for initialization
18 void Start ()
19 {
20 CreateTopSpawnPoints(8);
21 StartCoroutine("SpawningRoutine");
22 }
23
24 // CREATE POINTS FOR ENEMIES SPAWN BETWEEN strtSpawn AND endSpawn
25 /// <param name="pointsAmount"></param>
26 void CreateTopSpawnPoints(int pointsAmount)
27 {
28 pointsPositions = new float[pointsAmount];
29
30 float range = endSpawn.position.x - strtSpawn.position.x;
31
32 float onePointRange = range / pointsAmount;
33
34 pointsPositions[0] = strtSpawn.position.x + (onePointRange / 2);
35
36 for (int i = 1; i < pointsPositions.Length; i++)
37 {
38 pointsPositions[i] = pointsPositions[i - 1] + onePointRange;
39 }
40 }
41
42 IEnumerator SpawningRoutine()
43 {
44 yield return new WaitForSeconds(3);
45
46 while (!UIgame.scoreAchived)
47 {
48 GameObject enemy = null;
49
50 //FIRST STAGE OF SPAWNING
51 //CHOOSE ENEMY FOR SPAWN
52 int r = rand.Randomization_1(0, 10, 80, 99);
53 if (r == 0)
54 {
55 enemy = pools.GetPoolableObject("enemy_01");
56 }
57 else if (r == 1)
58 {
59 enemy = pools.GetPoolableObject("enemy_02");
60 }
61 else
62 {
63 enemy = pools.GetPoolableObject("enemy_03");
64 }
65
66 //SECOND STAGE OF SPAWNING
67 //SPAWN CHOSEN ENEMY IN RANDOM POINT ON TOP SCREEN SIDE
68 r = Random.Range(0, pointsPositions.Length);
69
70 if (enemy != null)
71 {
72 enemy.SetActive(true);
73 enemy.GetComponent<Enemy>().Activation(new Vector3(pointsPositions[r], 2, strtSpawn.position.z),
74 new Vector3(0,0,-1));
75 }
76
77 int spawnDelay;
78 if (SystemScr.difficultyIsHard)
79 spawnDelay = 1;
80 else
81 spawnDelay = Random.Range(3,5);
82
83 yield return new WaitForSeconds(spawnDelay);
84 }
85
86 //BOSS ACTIVATION
87 pools.DeleteEnemiesAndBullets();
88 CameraMotor.speedScreen = 0;
89 Camera.main.GetComponent<CameraMotor>().city_1.gameObject.SetActive(false);//remove city1
90 Camera.main.GetComponent<CameraMotor>().city_2.gameObject.SetActive(false);//remove city2
91 Camera.main.backgroundColor = Color.black;
92 GameObject.FindObjectOfType<SoundManager>().BossMusicOn();
93
94 yield return new WaitForSeconds(6);
95
96 pools.AcivateBoss();
97 }
98
99 }
2 using System.Collections;
3
4 public class SpawnManager : MonoBehaviour
5 {
6 public Transform strtSpawn, endSpawn;
7 float[] pointsPositions;
8 Pools pools;
9 Random rand;
10
11 void Awake()
12 {
13 pools = GameObject.FindObjectOfType<Pools>();
14 rand = new Random();
15 }
16
17 // Use this for initialization
18 void Start ()
19 {
20 CreateTopSpawnPoints(8);
21 StartCoroutine("SpawningRoutine");
22 }
23
24 // CREATE POINTS FOR ENEMIES SPAWN BETWEEN strtSpawn AND endSpawn
25 /// <param name="pointsAmount"></param>
26 void CreateTopSpawnPoints(int pointsAmount)
27 {
28 pointsPositions = new float[pointsAmount];
29
30 float range = endSpawn.position.x - strtSpawn.position.x;
31
32 float onePointRange = range / pointsAmount;
33
34 pointsPositions[0] = strtSpawn.position.x + (onePointRange / 2);
35
36 for (int i = 1; i < pointsPositions.Length; i++)
37 {
38 pointsPositions[i] = pointsPositions[i - 1] + onePointRange;
39 }
40 }
41
42 IEnumerator SpawningRoutine()
43 {
44 yield return new WaitForSeconds(3);
45
46 while (!UIgame.scoreAchived)
47 {
48 GameObject enemy = null;
49
50 //FIRST STAGE OF SPAWNING
51 //CHOOSE ENEMY FOR SPAWN
52 int r = rand.Randomization_1(0, 10, 80, 99);
53 if (r == 0)
54 {
55 enemy = pools.GetPoolableObject("enemy_01");
56 }
57 else if (r == 1)
58 {
59 enemy = pools.GetPoolableObject("enemy_02");
60 }
61 else
62 {
63 enemy = pools.GetPoolableObject("enemy_03");
64 }
65
66 //SECOND STAGE OF SPAWNING
67 //SPAWN CHOSEN ENEMY IN RANDOM POINT ON TOP SCREEN SIDE
68 r = Random.Range(0, pointsPositions.Length);
69
70 if (enemy != null)
71 {
72 enemy.SetActive(true);
73 enemy.GetComponent<Enemy>().Activation(new Vector3(pointsPositions[r], 2, strtSpawn.position.z),
74 new Vector3(0,0,-1));
75 }
76
77 int spawnDelay;
78 if (SystemScr.difficultyIsHard)
79 spawnDelay = 1;
80 else
81 spawnDelay = Random.Range(3,5);
82
83 yield return new WaitForSeconds(spawnDelay);
84 }
85
86 //BOSS ACTIVATION
87 pools.DeleteEnemiesAndBullets();
88 CameraMotor.speedScreen = 0;
89 Camera.main.GetComponent<CameraMotor>().city_1.gameObject.SetActive(false);//remove city1
90 Camera.main.GetComponent<CameraMotor>().city_2.gameObject.SetActive(false);//remove city2
91 Camera.main.backgroundColor = Color.black;
92 GameObject.FindObjectOfType<SoundManager>().BossMusicOn();
93
94 yield return new WaitForSeconds(6);
95
96 pools.AcivateBoss();
97 }
98
99 }
Use this for initialization
CREATE POINTS FOR ENEMIES SPAWN BETWEEN strtSpawn AND endSpawn
FIRST STAGE OF SPAWNING
CHOOSE ENEMY FOR SPAWN
SECOND STAGE OF SPAWNING
SPAWN CHOSEN ENEMY IN RANDOM POINT ON TOP SCREEN SIDE
BOSS ACTIVATION
Camera.main.GetComponent
Camera.main.GetComponent